﻿using MD5Hash;
using Microsoft.Extensions.Logging;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class UserDAL
    {
        public HoteContext HoteContent { get; }

        public UserDAL(HoteContext hoteContent)
        {
            HoteContent = hoteContent;
            
        }

       
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public  int UserAdd(UserModel info)
        {
            try
            {
                var list = HoteContent.Users.Count(o => o.UserName == info.UserName);
                if (list > 0)
                {
                    return -1;
                }
                HoteContent.Users.Add(info);
                return HoteContent.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        public int Login(string UserName,string UserPwd)
        {
            try
            {
                var user = HoteContent.Users.Where(o => o.UserName == UserName).Count();
                if (user == 0)
                {
                    return -1;
                }
                else
                {
                   
                    var erro=HoteContent.Users.Count(o => o.UserName == UserName && o.Password==UserPwd);
                    if (erro==0)
                    {
                        return -2;//密码错误
                    }
                    else
                    {
                       
                        return 1;//登陆成功
                    }
                }
            }
            catch (Exception)
            {
              
                throw;
            }
        }
    }
}
